package com.zkrt.upms.controller;

import com.baomidou.mybatisplus.extension.api.R;
import com.zkrt.upms.dto.DeviceAttrSaveDto;
import com.zkrt.upms.dto.DeviceAttrSaveMsDto;
import com.zkrt.upms.service.ITokenService;
import com.zkrt.upms.service.IUpDataService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.Date;

@Slf4j
@RestController
@RequestMapping("/data/upload")
public class DataUploadController {

    @Autowired
    private IUpDataService upDataService;

    @Autowired
    private ITokenService tokenService;

    @GetMapping("/get")
    public R get(@RequestHeader(required = false, value = "Authorization") String authorization) {
        String message = tokenService.verify(authorization);
        if (message != null) {
            return R.failed(message);
        }
        return upDataService.getHJ212();
    }

    @PostMapping("/test")
    public R test(@RequestHeader(required = false, value = "Authorization") String authorization) {
        String message = tokenService.verify(authorization);
        if (message != null) {
            return R.failed(message);
        }
        log.info("\n\n[测试推送开始] -> {}", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
        try {
            return upDataService.upload2Tcp();
        } finally {
            log.info("\n[测试推送结束] -> {}\n", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
        }
    }


    //手动补录数据接口
    @PostMapping("/toData")
    public R toData(@RequestBody DeviceAttrSaveDto saveDto) {

        return upDataService.toData(saveDto);

    }

    //手动补录数据接口-带毫秒值
    @PostMapping("/toDataMs")
    public R toDataMs(@RequestBody DeviceAttrSaveMsDto saveDto) {

        return upDataService.toDataMs(saveDto);

    }

}
